package com.example.service.impl;

import com.example.mapper.HeritageProjectMapper;
import com.example.pojo.HeritageProject;
import com.example.service.HeritageProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HeritageProjectServiceImpl implements HeritageProjectService {
    @Autowired
    private HeritageProjectMapper heritageProjectMapper;

    //获取所有非遗项目
    @Override
    public List<HeritageProject> getAllHeritageProjects() {
        return heritageProjectMapper.findAll();
    }

    //根据区域和物种获取所有非遗项目
    @Override
    public List<HeritageProject> findAllByCategory(Integer regionId, Integer speciesId) {
        return heritageProjectMapper.findAllByCategory(regionId, speciesId);
    }
    //增加非遗项目
    public void insert(HeritageProject project) {
        Integer regionId = heritageProjectMapper.findRegionIdByName(project.getRegionName());
        Integer speciesId = heritageProjectMapper.findSpeciesIdByName(project.getSpeciesName());
        project.setRegionId(regionId);
        project.setSpeciesId(speciesId);
        heritageProjectMapper.insert(project);
    }

    public void update(HeritageProject project) {
        Integer regionId = heritageProjectMapper.findRegionIdByName(project.getRegionName());
        Integer speciesId = heritageProjectMapper.findSpeciesIdByName(project.getSpeciesName());
        project.setRegionId(regionId);
        project.setSpeciesId(speciesId);
        heritageProjectMapper.update(project);
    }

    public void delete(Integer id) {
        heritageProjectMapper.delete(id);
    }

    public HeritageProject findById(Integer id) {
        return heritageProjectMapper.findById(id);
    }
}
